package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.squareup.okhttp.Request;

import java.io.Serializable;
import java.util.HashMap;

import butterknife.InjectView;
import cn.bingoogolapple.androidcommon.adapter.BGAOnItemChildClickListener;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.OrderDatailAdapter;
import project.dade.cn.bst.adapter.base.WashCarPhotoAdapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.model.OrderDatailModel;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.FullyLinearLayoutManager;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;

/**
 * Created by vain zhou on 2016/4/20.
 */
public class OrderDetailActivity extends ZJPBaseBarActivity implements View.OnClickListener , BGAOnRVItemClickListener,BGAOnItemChildClickListener {
    @InjectView(R.id.tv_order_name)
    TextView name;
    @InjectView(R.id.tv_order_phone)
    TextView phone;
    @InjectView(R.id.tv_address)
    TextView address;
    @InjectView(R.id.tv_sn)
    TextView orderdo;
    @InjectView(R.id.tv_order_time)
    TextView ordertime;
    @InjectView(R.id.tv_total_num)
    TextView totaltype;
    @InjectView(R.id.tv_liuyan)
    TextView  liuyan;
    @InjectView(R.id.listview_product)
    RecyclerView recyclerView;
    @InjectView(R.id.product_total_money)
    TextView totalmoey;
    @InjectView(R.id.total_money_count)
    TextView moneycount;
    @InjectView(R.id.tv_order_status)
    TextView orderstatus;
    @InjectView(R.id.recycler_view)
    RecyclerView getRecyclerView;
    @InjectView(R.id.layout_feedback)
    LinearLayout layout_feedback;
    @InjectView(R.id.tv_feedback)
    TextView feedback;
    @InjectView(R.id.layout_feedback_time)
    LinearLayout layout_feedbacktime;
    @InjectView(R.id.tv_feedbacktime)
    TextView getFeedback;
    @InjectView(R.id.layout_carno)
    LinearLayout carno;
    @InjectView(R.id.tv_carno)
    TextView tv_carno;//车牌号
    @InjectView(R.id.order_image_one)
    ImageView sumbit;
    @InjectView(R.id.order_text_one)
    TextView tv_sumbit;
    @InjectView(R.id.order_view_first)
    View view_one;
    @InjectView(R.id.order_image_two)
    ImageView payment;
    @InjectView(R.id.order_text_two)
    TextView tv_payment;
    @InjectView(R.id.order_view_two)
    View view_two;
    @InjectView(R.id.order_image_three)
    ImageView  deliver;
    @InjectView(R.id.order_view_three)
    View view_three;
    @InjectView(R.id.order_text_three)
    TextView tv_deliver;
    @InjectView(R.id.order_image_four)
    ImageView complete;
    @InjectView(R.id.order_text_four)
    TextView tv_complete;
    @InjectView(R.id.order_status_text)
    TextView getOrderstatus;
    private OrderDatailAdapter orderDatailAdapter;
    private  String orderno;
    public OrderDatailModel orderDatailModel;
    private WashCarPhotoAdapter washCarPhotoAdapter;
    private KProgressHUD hud;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetail);
         FullyLinearLayoutManager  fullyLinearLayoutMana=new FullyLinearLayoutManager(OrderDetailActivity.this);
        fullyLinearLayoutMana.setOrientation(LinearLayoutManager.HORIZONTAL);
        getRecyclerView.setLayoutManager(fullyLinearLayoutMana);
        washCarPhotoAdapter=new WashCarPhotoAdapter(getRecyclerView);
        FullyLinearLayoutManager fullyLinearLayoutManager =new FullyLinearLayoutManager(OrderDetailActivity.this);
        fullyLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(fullyLinearLayoutManager);
        orderDatailAdapter=new OrderDatailAdapter(recyclerView);
        recyclerView.setAdapter(orderDatailAdapter);
        orderno=getIntent().getStringExtra("orderno");
        orderDatailAdapter.setOnRVItemClickListener(this);
        orderDatailAdapter.setOnItemChildClickListener(this);
        washCarPhotoAdapter.setOnRVItemClickListener(this);
        requestGetOrderDatil();
    }

    private void requestGetOrderDatil() {
        hud = KProgressHUD.create(OrderDetailActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在加载")
                .setCancellable(true);
        hud.show();
        final HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", AppApplication.getInstance().getSpUtil().getUserID().toString());
        params.put("mobilephone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        params.put("orderno",orderno);
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Ordertail).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                          hud.dismiss();
                        ToastUtil.showShort(OrderDetailActivity.this, "网络相应失败请稍后再试");
                    }
                    @Override
                    public void onResponse(String response) {
                        hud.dismiss();
                        LogUtil.i(TAG, "获取订单列表：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取订单列表"))) {
                           OrderDatailModel model = JSON.parseObject(response, OrderDatailModel.class);
                            orderDatailModel=model;
                            LogUtil.i(TAG, "获取订单详情：" + model.getData());
                            name.setText(model.getData().getConsigneeName());
                            phone.setText(model.getData().getConsigneePhone());
                            address.setText(model.getData().getConsigneeAddress());
                            orderdo.setText(model.getData().getOrderNo());
                            ordertime.setText(model.getData().getOrderDate());
                            totaltype.setText(model.getData().getPaymentType()+"");
                            liuyan.setText(model.getData().getRemark());
                            totalmoey.setText(model.getData().getFactPrice()+"");
                            moneycount.setText(model.getData().getFactPrice()+"");
                            orderDatailAdapter.setDatas(model.getData().getOrderProducts());
                            if(model.getData().getOrderFeedBack()==null){

                            }else {
                                layout_feedback.setVisibility(View.VISIBLE);
                                layout_feedbacktime.setVisibility(View.VISIBLE);
                                carno.setVisibility(View.VISIBLE);
                                tv_carno.setText(model.getData().getOrderFeedBack().getCarNo());
                                feedback.setText(model.getData().getOrderFeedBack().getContent());
                                getFeedback.setText(model.getData().getOrderFeedBack().getFeedBackTime());
                                washCarPhotoAdapter.setDatas(model.getData().getOrderFeedBack().getImages());
                                getRecyclerView.setAdapter(washCarPhotoAdapter);

                            }
                            if (model.getData().getOrderStatus() == 7/*已经取消*/ || model.getData().getOrderStatus() == 5/*交易完成*/ || model.getData().getOrderStatus() == 8/*交易关闭*/ || model.getData().getOrderStatus() == 9/*退款处理中*/) {
                                if (model.getData().getOrderStatus() == 7) {
                                    tv_sumbit.setText("已取消");
                                    getOrderstatus.setText("订单已被取消");
                                }
                                if (model.getData().getOrderStatus() == 8) {
                                    tv_sumbit.setText("交易关闭");
                                    getOrderstatus.setText("订单已被关闭");

                                }
                                if (model.getData().getOrderStatus() == 5) {
                                    getOrderstatus.setText("订单已完成");
                                    view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    payment.setImageResource(R.mipmap.two);
                                    tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    view_two.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    view_three.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    deliver.setImageResource(R.mipmap.three);
                                    tv_deliver.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    complete.setImageResource(R.mipmap.four);
                                    tv_complete.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                }
                                if (model.getData().getOrderStatus() == 9) {
                                    getOrderstatus.setText("退货处理中");
                                    view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    payment.setImageResource(R.mipmap.two);
                                    tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    view_two.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    deliver.setImageResource(R.mipmap.three);
                                    tv_deliver.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                    tv_deliver.setText("退货处理中");
                                }

                            } else {
                                if (model.getData().getPaymentStatus() == 1/*已经付款*/) {
                                    //判断订单是否已经被确认
                                    if (model.getData().getOrderStatus() == 0/*等待确认*/) {
                                        view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                        payment.setImageResource(R.mipmap.two);
                                        tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                        view_two.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                        deliver.setImageResource(R.mipmap.three);
                                        tv_deliver.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                        getOrderstatus.setText("订单等待确认中");
                                        tv_deliver.setText("等待确认");
                                    }
                                    if (model.getData().getOrderStatus() == 2/*已经确认*/) {

                                        //继续取物流状态
                                        if (model.getData().getOgisticsStatus() == 0) {
                                            getOrderstatus.setText("订单配货中");
                                            view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            payment.setImageResource(R.mipmap.two);
                                            tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            view_two.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            deliver.setImageResource(R.mipmap.three);
                                            tv_deliver.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            tv_deliver.setText("配货中");
                                        }
                                        if (model.getData().getOgisticsStatus() == 1) {
                                            getOrderstatus.setText("订单配送中");
                                            view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            payment.setImageResource(R.mipmap.two);
                                            tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            view_two.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            deliver.setImageResource(R.mipmap.three);
                                            tv_deliver.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            tv_deliver.setText("配送中");

                                        }
                                        if (model.getData().getOgisticsStatus() == 3) {
                                            getOrderstatus.setText("未发货");
                                            view_one.setBackgroundColor(android.graphics.Color.parseColor("#FF1F9DF6"));
                                            payment.setImageResource(R.mipmap.two);
                                            tv_payment.setTextColor(android.graphics.Color.parseColor("#FF1F9DF6"));

                                        }
                                    }
                                }
                                if (model.getData().getPaymentStatus() == 0/*等待付款*/) {

                                }
                            }

                        }else {
                          hud.dismiss();
                            ToastUtil.showShort(OrderDetailActivity.this, NetRequestUtil.requestsError(response, "获取订单列表"));

                        }
                    }

                });

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemChildClick(ViewGroup viewGroup, View childView, int postion) {
        if (childView.getId() == R.id.btn_order_comment) {
            if (orderDatailAdapter.getItem(postion).getIsSigned() == 1 && orderDatailAdapter.getItem(postion).getCommentType() == 0) {
                Intent mIntent = new Intent(OrderDetailActivity.this,
                        CommentActivity.class);
                mIntent.putExtra("orderno", orderDatailModel.getData().getOrderNo());
                mIntent.putExtra("orderimage", orderDatailAdapter.getItem(postion).getProductImage());
                mIntent.putExtra("price", orderDatailAdapter.getItem(postion).getPrice());
                mIntent.putExtra("title", orderDatailAdapter.getItem(postion).getProductName());
                mIntent.putExtra("id", orderDatailAdapter.getItem(postion).getProductId());
                startActivity(mIntent);
                finish();
            } else {
                Intent mIntent = new Intent(OrderDetailActivity.this,
                        RefundActivity.class);
                mIntent.putExtra("orderno",orderDatailModel.getData().getOrderNo());
                mIntent.putExtra("orderimage", orderDatailAdapter.getItem(postion).getProductImage());
                mIntent.putExtra("price", orderDatailAdapter.getItem(postion).getPrice() + "");
                mIntent.putExtra("title", orderDatailAdapter.getItem(postion).getProductName());
                mIntent.putExtra("id", orderDatailAdapter.getItem(postion).getProductId());
                mIntent.putExtra("num", orderDatailAdapter.getItem(postion).getQuantity() + "");
                startActivity(mIntent);
                finish();
            }


        }
    }

    @Override
    public void onRVItemClick(ViewGroup viewGroup, View view, int postion) {
        if (viewGroup.getId() == R.id.recycler_view) {
            Intent intent = new Intent(OrderDetailActivity.this, ImageGalleryActivity.class);
            intent.putExtra("images", (Serializable) orderDatailModel.getData().getOrderFeedBack().getImages());
            intent.putExtra("position", postion);
            startActivity(intent);

        }



    }


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
